<?php
/**
 * SimplifyPHP Framework
 *
 * This file is part of SimplifyPHP Framework.
 *
 * SimplifyPHP Framework is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * SimplifyPHP Framework is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author Rodrigo Rutkoski Rodrigues, <rutkoski@gmail.com>
 * @copyright Copyright 2008 Rodrigo Rutkoski Rodrigues
 */

require_once('html_mime_mail/htmlMimeMail5.php');

/**
 *
 * @author Rutkoski
 *
 */
class MailComponent extends Component
{
	/**
	 *
	 */
	protected $error = false;
	/**
	 *
	 */
	protected static $mail;
	/**
	 *
	 */
	public function getError()
	{
		return $this->error;
	}
	/**
	 *
	 */
	public function send($to, $subject, $data)
	{
		if (!$this->has('mail_from')) {
			$this->error = __('Missing mail_from parameter.');
			throw new ParameterException($this->error);
		}

    if (!$this->has('html_template')) {
      $this->error = __('Missing html_template parameter.');
      throw new ParameterException($this->error);
    }

		$data = DataHolder::parseAsDataHolder($data);

		$htmlTpl = $this->get('html_template');
		$textTpl = $this->get('text_template');

		if ($this->get('render_string')) {
			$this->renderString($data, $htmlTpl, $textTpl);
		} else {
			$this->renderTemplate($data, $htmlTpl, $textTpl);
		}

		$mail = $this->getMail();
		$mail->setFrom($this->get('mail_from'));
		$mail->setSubject($subject);

		if (!is_array($to)) $to = array($to);

		$sent = $mail->send($to, strtolower($this->get('mail_engine', 'mail')));

		if (false === $sent) {
			$this->error = $mail->errors;
			throw new Exception($this->error);
		}
	}
	/**
	 *
	 */
	protected function renderTemplate($data, $htmlTpl, $textTpl = null)
	{
		$html = View::factory('PHP');
		$html->setTemplate($htmlTpl);
		$html->setLayout(false);
		$html->copyData($data);
		$html = $html->render(null, View::RENDER_VAR);

		if (empty($textTpl)) {
			$crlf = "\r\n";
			$text = $html;
			$text = preg_replace('#< */ *p *>|< *br */? *>#i', $crlf, $text);
			$text = strip_tags($text);
		} else {
			$text = View::factory('PHP');
			$text->setTemplate($textTpl);
			$text->setLayout(false);
			$text->copyData($data);
			$text = $text->render(null, View::RENDER_VAR);
		}

		$mail = $this->getMail();
		$mail->setHTML($html);
		$mail->setText($text);
	}
	/**
	 *
	 */
	protected function renderString($data, $html, $text = null)
	{
		foreach ($data->getAll() as $k=>$v) {
			$html = str_replace('{'.$k.'}', $v, $html);

			if (!empty($text)) {
				$text = str_replace('{'.$k.'}', $v, $text);
			}
		}

		if (empty($text)) {
			$crlf = "\r\n";
			$text = $html;
			$text = preg_replace('#< */ *p *>|< *br */? *>#i', $crlf, $text);
			$text = strip_tags($text);
		}

		$mail = $this->getMail();
		$mail->setHTML($html);
		$mail->setText($text);
	}
	/**
	 *
	 */
	protected function getMail()
	{
		if (empty(self::$mail)) {
			$this->mail = new htmlMimeMail5();
			$this->mail->setCRLF("\r\n");
			$this->mail->setTextCharset('UTF-8');
			$this->mail->setHTMLCharset('UTF-8');
			$this->mail->setHeadCharset('UTF-8');
			$this->mail->setPriority('high');

			if ($this->get('mail_engine') == 'SMTP') {
				$this->mail->setSMTPParams(
				$this->get('mail_smtp_host'),
				$this->get('mail_smtp_port'),
				null,
				$this->get('mail_smtp_auth'),
				$this->get('mail_smtp_user'),
				$this->get('mail_smtp_pass')
				);
			}
		}

		return self::$mail;
	}
}

?>